﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace XFFSM
{
    public class FSMTransitionFactory 
    {

        public static void CreateTransition(RuntimeFSMController controller,string fromStateName,string toStateName) {

            if ( toStateName.Equals(FSMConst.anyState) || toStateName.Equals(FSMConst.entryState) ) {
                return;
            }

            if (fromStateName.Equals(toStateName)) {
                return;
            }

            foreach (var item in controller.transitions)
            {
                if (item.fromStateName.Equals(fromStateName) && item.toStateName.Equals(toStateName) ) {
                    Debug.LogErrorFormat("过渡 {0} -> {1} 已经存在,请勿重复添加!", fromStateName, toStateName);
                    return;
                }
            }

            FSMTransitionData transition = new FSMTransitionData();
            transition.fromStateName = fromStateName;
            transition.toStateName = toStateName;
            controller.transitions.Add(transition);
            UnityEditor.EditorUtility.SetDirty(controller);
            UnityEditor.AssetDatabase.SaveAssets();

        }

        public static void DeleteTransition(RuntimeFSMController controller,FSMTransitionData transition) {
            controller.transitions.Remove(transition);
            UnityEditor.EditorUtility.SetDirty(controller);
            UnityEditor.AssetDatabase.SaveAssets();
        }

    }

}

